﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XCSerialPort
{
    public class Pageactions : IPageactions
    {
        /// <summary>
        /// 页面寄存器下发
        /// </summary>
        /// <param name="sender">控件信息</param>
        /// <param name="listadd">模块寄存器集合</param>
        /// <param name="ModeName">模式</param>
        public string PageAddrSet(object sender, List<addressregister> listaddSum, string ModeName = null)
        {
            var text = sender as TextBox;
            if (string.IsNullOrEmpty(text.Text)) return "";
            IBitsHelper bitsHelper = new BitsHelp();
            var listSum = listaddSum.Where(c => c.NameType.ToLower().Contains(text.Name.ToString().ToLower())).ToList();
            List<addressregister> listadd = new List<addressregister>();
            if (!string.IsNullOrEmpty(ModeName))
            {
                listadd = listSum.Where(c => c.NameType.ToLower().Contains(ModeName.ToLower())).ToList();
            }
            string str = text.Name + "\n\r";
            if (0 != listadd.Count && !string.IsNullOrEmpty(ModeName))
            {
                if (listadd.Exists(c => c.NameType.ToLower() == (text.Name + ModeName).ToLower()))
                {
                    var model = listadd.Find(c => c.NameType.ToLower() == (text.Name + ModeName).ToLower());
                    var Val = bitsHelper.SetBitsToUint(model.Bits, model.Hex, text.Text);
                    //SetHDRRegister(model, Val);
                    str += "0x" + model.Address + " 0x" + Val.ToString("X2");
                }
                if (listadd.Exists(c => c.NameType.ToLower() == (text.Name + "_h" + ModeName).ToLower()))
                {
                    var model = listadd.Find(c => c.NameType.ToLower() == (text.Name + "_h" + ModeName).ToLower());
                    var Val = bitsHelper.SetBitsToUint(model.Bits, model.Hex, text.Text);
                    //SetAeRegister(model, Val);
                    str += "0x" + model.Address + " 0x" + Val.ToString("X2");
                }
                if (listadd.Exists(c => c.NameType.ToLower() == (text.Name + "_m1" + ModeName).ToLower()))
                {
                    var model = listadd.Find(c => c.NameType.ToLower() == (text.Name + "_m1" + ModeName).ToLower());
                    var Val = bitsHelper.SetBitsToUint(model.Bits, model.Hex, text.Text);
                    //SetAeRegister(model, Val);
                    str += "\n\r" + "0x" + model.Address + " 0x" + Val.ToString("X2");
                }
                if (listadd.Exists(c => c.NameType.ToLower() == (text.Name + "_m0" + ModeName).ToLower()))
                {
                    var model = listadd.Find(c => c.NameType.ToLower() == (text.Name + "_m0" + ModeName).ToLower());
                    var Val = bitsHelper.SetBitsToUint(model.Bits, model.Hex, text.Text);
                    //SetAeRegister(model, Val);
                    str += "\n\r" + "0x" + model.Address + " 0x" + Val.ToString("X2");
                }
                if (listadd.Exists(c => c.NameType.ToLower() == (text.Name + "_l" + ModeName).ToLower()))
                {
                    var model = listadd.Find(c => c.NameType.ToLower() == (text.Name + "_l" + ModeName).ToLower());
                    var Val = bitsHelper.SetBitsToUint(model.Bits, model.Hex, text.Text);
                    //SetAeRegister(model, Val);
                    str += "\n\r" + "0x" + model.Address + " 0x" + Val.ToString("X2");
                }
            }
            else
            {
                if (listSum.Exists(c => c.NameType.ToLower() == (text.Name).ToLower()))
                {
                    var model = listSum.Find(c => c.NameType.ToLower() == (text.Name).ToLower());
                    var Val = bitsHelper.SetBitsToUint(model.Bits, model.Hex, text.Text);
                    //SetHDRRegister(model, Val);
                    str += "0x" + model.Address + " 0x" + Val.ToString("X2");
                }
                if (listSum.Exists(c => c.NameType.ToLower() == (text.Name + "_h").ToLower()))
                {
                    var model = listSum.Find(c => c.NameType.ToLower() == (text.Name + "_h").ToLower());
                    var Val = bitsHelper.SetBitsToUint(model.Bits, model.Hex, text.Text);
                    //SetAeRegister(model, Val);
                    str += "0x" + model.Address + " 0x" + Val.ToString("X2");
                }
                if (listSum.Exists(c => c.NameType.ToLower() == (text.Name + "_m1").ToLower()))
                {
                    var model = listSum.Find(c => c.NameType.ToLower() == (text.Name + "_m1").ToLower());
                    var Val = bitsHelper.SetBitsToUint(model.Bits, model.Hex, text.Text);
                    //SetAeRegister(model, Val);
                    str += "\n\r" + "0x" + model.Address + " 0x" + Val.ToString("X2");
                }
                if (listSum.Exists(c => c.NameType.ToLower() == (text.Name + "_m0").ToLower()))
                {
                    var model = listSum.Find(c => c.NameType.ToLower() == (text.Name + "_m0").ToLower());
                    var Val = bitsHelper.SetBitsToUint(model.Bits, model.Hex, text.Text);
                    //SetAeRegister(model, Val);
                    str += "\n\r" + "0x" + model.Address + " 0x" + Val.ToString("X2");
                }
                if (listSum.Exists(c => c.NameType.ToLower() == (text.Name + "_l").ToLower()))
                {
                    var model = listSum.Find(c => c.NameType.ToLower() == (text.Name + "_l").ToLower());
                    var Val = bitsHelper.SetBitsToUint(model.Bits, model.Hex, text.Text);
                    //SetAeRegister(model, Val);
                    str += "\n\r" + "0x" + model.Address + " 0x" + Val.ToString("X2");
                }
            }
            return str;
        }
    }
}
